#pragma once
#include <iostream>
#include <unistd.h>
#include <cstdlib>

using namespace std;

typedef void(*work_t)(int);//函数指针类型
typedef void(*task_t)(int, pid_t);//函数指针类型

void Func1(int fd, pid_t pid)
{
    cout<<"pid: "<<pid<<", fd: "<<fd<<", task is: 任务1\n"<<endl;
    cout<<endl;
}
void Func2(int fd, pid_t pid)
{
    cout<<"pid: "<<pid<<", fd: "<<fd<<", task is: 任务2\n"<<endl;
    cout<<endl;
}
void Func3(int fd, pid_t pid)
{
    cout<<"pid: "<<pid<<", fd: "<<fd<<", task is: 任务3\n"<<endl;
    cout<<endl;
}
uint32_t commandNext()
{
    return rand() % 3;
}

task_t tasks[3] = {Func1, Func2, Func3};

void worker(int fd)//任务
{
    while(true)
    {
        uint32_t command_code = 0;
        //读
        ssize_t n = read(0, &command_code, sizeof(command_code));//有重定向直接在0读取
        if(n == sizeof(command_code))
        {
            if(command_code >= 3) continue;
            tasks[command_code](fd, getpid());
        }
        else if(n == 0)//读到返回值为0，退出
        {
            cout<<"sub process: "<<getpid()<<" qiut now..."<<endl;
            break;
        }
    }
}